package com.unina.dropplayer.play;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;

import com.dropbox.client2.DropboxAPI;
import com.dropbox.client2.android.AndroidAuthSession;
import com.example.dropplayer.R;
import com.unina.dropplayer.common.DropPlayerConstants;
import com.unina.dropplayer.common.GetFileListTask;
import com.unina.dropplayer.utilities.DropPlayerSessionBuilder;

import android.app.ListActivity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class PlayListActivity extends ListActivity {
	// Songs list
	private ArrayList<SongInfo> songsList = new ArrayList<SongInfo>();
	private GetFileListTask getFileListTask;
	private DropboxAPI<AndroidAuthSession> mDBApi;
	private ListAdapter adapter;
	private ListView listView;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		HashMap<String, String> song;
		ArrayList<HashMap<String, String>> songsListData;
		
		super.onCreate(savedInstanceState);
		
		AndroidAuthSession session = DropPlayerSessionBuilder
				.buildSession(this);
		mDBApi = new DropboxAPI<AndroidAuthSession>(session);
		
		setContentView(R.layout.playlist);
		
		// selecting single ListView item
				listView = getListView();
				// listening to single listitem click
				listView.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {
						// getting listitem index
						int songIndex = position;
						
						// Starting new intent
						Intent in = new Intent(getApplicationContext(),
								PlayListActivity.class);
						// Sending songIndex to PlayerActivity
						in.putExtra("songIndex", songIndex);
						setResult(100, in);
						// Closing PlayListView
						finish();
					}
				});

		songsListData = new ArrayList<HashMap<String, String>>();
		
		
		// Getting all songs list
		getFileListTask = new GetFileListTask(this, mDBApi,
				DropPlayerConstants.DROP_PLAYER_ROOT_FOLDER, listView, adapter);
		try {
			songsList = getFileListTask.execute().get();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		// looping through playlist
		for (SongInfo songInfo : songsList) {
			song = new HashMap<String, String>();
			song.put(songInfo.getSongPath(), songInfo.getSongTitle());
		}
		
		
		// Adding menuItems to ListView
		ListAdapter adapter = new SimpleAdapter(this, songsListData,
				R.layout.playlist_item, new String[] { "songTitle" }, new int[] {
						R.id.songTitle });

		setListAdapter(adapter);

		

	}
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		//setContentView(R.layout.playlist);
	}
}
